package com.iqiju.guagua.redtone.video.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iqiju.guagua.redtone.video.mapper.WxVideoTypesMapper;
import com.iqiju.guagua.redtone.video.model.WxMiniVideo;
import com.iqiju.guagua.redtone.video.model.WxVideoTypes;
import com.iqiju.guagua.redtone.video.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class WxVideoTypesService extends ServiceImpl<WxVideoTypesMapper, WxVideoTypes> {

    @Autowired
    private WxMiniVideoService wxMiniVideoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final Map<String,String> REDTONE_VIDEO_IDS_LOCAL_LIST = new HashMap<>(16);


    /** 根据自定义视频类型id查询所有的视频ids */
    public List<Long> selectVideoIdsByLocalTypeId(long localTypeId) {
        //定义 local 类型的所有视频ids
        List<Long> videoIds = null;
        //map中的视频ids对应的key
        String key = getLocalTypeVideoIdKey(localTypeId);
        //本地自定义类型对应的好看视频类型所有id
        List<Long> haokanTypeIds = null;
        //从缓存中查询本地视频类型对应的所有好看视频类型ids
        String haoIdsValue = stringRedisTemplate.opsForValue().get(haoIdsKey(localTypeId));

        if (haoIdsValue==null||haoIdsValue.trim().length()<=0){
            //缓存中不存在
            //首先删除map中本地视频类型id对应的所有视频ids
            REDTONE_VIDEO_IDS_LOCAL_LIST.remove(key);
            //从数据库查询本地视频类型对应的所有好看视频类型ids
            haokanTypeIds = selectHaoKanTypeIdsByLocalTypeId(localTypeId);
            //将从数据库中查到的好看视频类型ids存储到redis中
            stringRedisTemplate.opsForValue().set(haoIdsKey(localTypeId), JSON.toJSONString(haokanTypeIds),30, TimeUnit.DAYS);
        }else{
            //缓存中存在
            //从map中获取本地视频类型对应的所有视频ids
            String listValue = REDTONE_VIDEO_IDS_LOCAL_LIST.get(key);
            //如果map中存在，就直接返回
            if (StringUtils.isNotNull(listValue)){
                videoIds = JSON.parseArray(listValue,Long.class);
                return videoIds;
            }
            //如果map中不存在，就从redis的值中获取本地视频类型对应的好看视频类型ids
            haokanTypeIds = JSON.parseArray(haoIdsValue,Long.class);
        }

        //根据好看视频类型ids从数据库中查询所有的视频ids
        videoIds = selectIdsByHaokanTypeIds(haokanTypeIds);
        //将查询到的视频ids放入map中，redis缓存不删除，就一直使用map中的ids
        REDTONE_VIDEO_IDS_LOCAL_LIST.put(key,JSON.toJSONString(videoIds));
        //返回最终结果（本地视频类型对应的所有视频ids）
        return videoIds;
    }

    private String haoIdsKey(long localTypeId){
        return "REDTONE_VIDEO_HAO_IDS_" + localTypeId;
    }

















    private String getLocalTypeVideoIdKey(long localTypeId){
        return "REDTONE_VIDEO_IDS_LOCAL_LIST_"+localTypeId;
    }



    /** 查询所有自定义视频类型id 对应的 好看视频类型id */
    private List<Long> selectHaoKanTypeIdsByLocalTypeId(long localTypeId) {
        QueryWrapper<WxVideoTypes> wrapper = new QueryWrapper<>();
        wrapper.eq("wx_redtone_video_type_id",localTypeId);
        List<WxVideoTypes> list = list(wrapper);
        List<Long> ids = new ArrayList<>();
        for (WxVideoTypes wxVideoTypes : list) {
            ids.add(wxVideoTypes.getWxMiniVideoTypeId());
        }
        return ids;
    }


    /** 根据好看视频类型ids获取所有的视频ids */
    private List<Long> selectIdsByHaokanTypeIds(List<Long> haokanTypeIds) {
        QueryWrapper<WxMiniVideo> queryWrapper = new QueryWrapper<>();
        for (Long haokanTypeId : haokanTypeIds) {
            queryWrapper.or().eq("type",haokanTypeId);
        }
        List<WxMiniVideo> list = wxMiniVideoService.list(queryWrapper);
        List<Long> ids = new ArrayList<>();
        for (WxMiniVideo wxMiniVideo : list) {
            ids.add(wxMiniVideo.getId());
        }
        return ids;
    }

}
